package com.study.talk.module.netty.handler;

import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.InviteGroupRequest;
import com.study.talk.module.netty.message.response.InviteGroupResponse;
import com.study.talk.module.netty.message.response.InviteGroupSelfResponse;
import com.study.talk.module.service.*;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 邀请加入群聊消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class InviteGroupRequestHandler extends SimpleChannelInboundHandler<InviteGroupRequest> {

    @Resource
    private GroupInfoService groupInfoService;

    @Resource
    private GroupUserInfoService groupUserInfoService;

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private InviteGroupRecordService inviteGroupRecordService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, InviteGroupRequest msg) throws Exception {
        log.info("邀请好友加入群聊事件,{}",msg);

        InviteGroupSelfResponse inviteGroupSelfResponse = new InviteGroupSelfResponse();
        GroupInfo groupInfo = groupInfoService.getById(msg.getGroupId());
        Session session = ctx.channel().attr(Attributes.SESSION).get();
        inviteGroupSelfResponse.setGroupName(groupInfo.getName());
        inviteGroupSelfResponse.setGroupId(groupInfo.getId());
        inviteGroupSelfResponse.setDateTime(msg.getDateTime());
        List<UserInfo> successList = new ArrayList<>();
        List<UserInfo> failList = new ArrayList<>();

        InviteGroupResponse inviteGroupResponse = new InviteGroupResponse();
        inviteGroupResponse.setInviteUserId(session.getUserId());
        inviteGroupResponse.setInviteUserName(session.getUserName());
        inviteGroupResponse.setGroupName(groupInfo.getName());
        inviteGroupResponse.setGroupId(groupInfo.getId());
        inviteGroupResponse.setDateTime(msg.getDateTime());


        for (Integer id : msg.getUserList()){
            if (groupUserInfoService.queryHasMember(msg.getGroupId(),id)){
                failList.add(userInfoService.getById(id));
                continue;
            }
            successList.add(userInfoService.getById(id));
            if (!inviteGroupRecordService.queryHasInviteMember(groupInfo.getId(), session.getUserId(), id)){
                inviteGroupRecordService.addInviteMember(groupInfo.getId(),session.getUserId(),id);
            }
            userService.sendMessage(id,inviteGroupResponse);
        }

        inviteGroupSelfResponse.setFailedUser(failList);
        inviteGroupSelfResponse.setSuccessUser(successList);
        userService.sendMessage(session.getUserId(),inviteGroupSelfResponse);
    }
}
